package com.example.copa_america;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.copa_america.Fixtures;
import com.example.copa_america.Noticias.MyAdapter;
import com.example.copa_america.dao.DataServiceFixtures;
import com.example.copa_america.model.Fixture;
import com.example.copa_america.model.Noticia;

import android.app.ListActivity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class Fixtures extends ListActivity {
	
	private static final String URL_TEST_FIXTURES = "http://192.168.0.101/Ultima/proyecto2/partidos.xml";
	private MyAdapter mAdapter = null;
	
	private static ArrayList<Fixture> partidos;
	
	
	public void onCreate(Bundle savedInstanceState){ 

		super.onCreate(savedInstanceState);
		//TextView textoNuevo = (TextView)findViewById(R.id.mostradorfixtures);
		//textoNuevo.setText(fixtures1.get(1).getNombre().toString());
		DataServiceFixtures dataServiceFixtures = new DataServiceFixtures();
		ArrayList<Fixture> fixtures2 = dataServiceFixtures.getFixtures(URL_TEST_FIXTURES);
		
		
		partidos = fixtures2;
		mAdapter = new MyAdapter(this);
		setListAdapter(mAdapter);
		
	}

	
	
	public static class MyAdapter extends BaseAdapter{
		
		private Context mContext;
		
		public MyAdapter(Context c){
			mContext = c;
		}
		
		@Override
		//especifica el numero de items
		public int getCount() {
			// TODO Auto-generated method stub
			return partidos.size()/2;
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			
			return partidos.get(position*2);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return 0;
		}
		//devuelve las vistas
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			
			View view = null;
			if(convertView==null){
				LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				view = inflater.inflate(R.layout.listafixtures, null);
			} else {
				view = convertView;
			}
			
			TextView equipo1 = (TextView) view.findViewById(R.id.nombre2);
			equipo1.setText(partidos.get(position*2).getNombre());
			
			TextView equipo2 = (TextView) view.findViewById(R.id.nombre);
			equipo2.setText(partidos.get((position*2)+1).getNombre());
			
			
			TextView fecha = (TextView) view.findViewById(R.id.fecha);
			fecha.setText(partidos.get((position*2)+1).getFecha());
			

			TextView hora = (TextView) view.findViewById(R.id.hora);
			hora.setText(partidos.get((position*2)+1).getHora());
			
			TextView estadio = (TextView) view.findViewById(R.id.estadio);
			estadio.setText(partidos.get(position).getEstadio());
			
			
			
			
			return view;
		}
		
	}



	

}
